package gw.com.android.ui.quote.view;

import android.content.Context;
import android.support.v4.app.FragmentActivity;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.gwtsz.gts2.hx.R;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import gw.com.android.app.GTConfig;
import gw.com.android.utils.MobclickEventUtlis;
import gw.com.android.utils.PackStatis;
import www.com.library.util.CommonUtils;
import www.com.library.view.TintImageView;

/**
 * 行情底部账号切换
 * @author Reeta.zou
 *
 */
public class AccountChangeView extends LinearLayout{
	@BindView(R.id.one_layout)
	LinearLayout mOneLayout;
	@BindView(R.id.two_layout)
	LinearLayout mTwoLayout;
	@BindView(R.id.tv_up)
	TextView mUpView;
	@BindView(R.id.tv_down)
	TextView mDownView;
	@BindView(R.id.arrow_up)
    TintImageView mArrowup;
	private UserInfoHBar.AccountChangeViewClickListener mCallback;// 切换按钮点击事件监听器
	private FragmentActivity mOwnerAct;

	public AccountChangeView(Context context) {
		super(context);
		initView(context);
	}

	public AccountChangeView(Context context, AttributeSet attrs) {
		super(context, attrs);
		initView(context);
	}

	public AccountChangeView(Context context, AttributeSet attrs,
							 int defStyleAttr) {
		super(context, attrs, defStyleAttr);
		initView(context);
	}

	private void initView(Context context) {
		mOwnerAct = (FragmentActivity) context;
		View mView = LayoutInflater.from(context).inflate(R.layout.account_change_view, this, true);
		ButterKnife.bind(this, mView);
		initData();
	}

	public void initData() {
		if (GTConfig.instance().getAccountType() == GTConfig.ACC_TYPE_GUEST) {
			mUpView.setText(mOwnerAct.getString(R.string.quote_title_tourist));
			mOneLayout.setVisibility(View.VISIBLE);
			mTwoLayout.setVisibility(View.GONE);
		} else if (GTConfig.instance().getAccountType() == GTConfig.ACC_TYPE_DEMO) {
			mDownView.setText(mOwnerAct.getString(R.string.quote_title_demo));
			mUpView.setText(mOwnerAct.getString(R.string.quote_title_real));
			mOneLayout.setVisibility(View.GONE);
//			mOneLayout.setBackgroundColor(ColorThemeUtil.instance().color_accinfo_c);
			mTwoLayout.setVisibility(View.VISIBLE);
		} else {
			mDownView.setText(mOwnerAct.getString(R.string.quote_title_real));
			mUpView.setText(mOwnerAct.getString(R.string.quote_title_demo));
			mOneLayout.setVisibility(View.GONE);
			mTwoLayout.setVisibility(View.VISIBLE);
		}
	}

	public void refreshView(){
		initData();
		mArrowup.setImageResource(R.mipmap.a_small_developup);
	}

	@OnClick(R.id.one_layout)
	void onGuestClick(){
		if(CommonUtils.isFastDoubleClick()){
			return;
		}
		if (GTConfig.instance().getAccountType() == GTConfig.ACC_TYPE_GUEST) {
			return;
		}
		if(GTConfig.instance().getAccountType() == GTConfig.ACC_TYPE_DEMO){
//			GTConfig.instance().setAccountType(GTConfig.ACC_TYPE_REAL);
			mCallback.onViewClick(GTConfig.ACC_TYPE_REAL);
			MobclickEventUtlis.MobclickEventByAccountType(mOwnerAct,"ClickChangeToReal");
			PackStatis.getToService(PackStatis.EventAction.CLICKCHANGETOREAL.getValue(),PackStatis.EventCategory.MARKET.getValue(),null,null);
		}else{
//			GTConfig.instance().setAccountType(GTConfig.ACC_TYPE_DEMO);
			mCallback.onViewClick(GTConfig.ACC_TYPE_DEMO);
			MobclickEventUtlis.MobclickEventByAccountType(mOwnerAct,"ClickChangeToDemo");
			PackStatis.getToService(PackStatis.EventAction.CLICKCHANGETODEMO.getValue(),PackStatis.EventCategory.MARKET.getValue(),null,null);
		}
//		initData();
//		ActivityManager.backLogin(mOwnerAct, false);
	}

	@OnClick(R.id.two_layout)
	void onRealClick(){
		if(CommonUtils.isFastDoubleClick()){
			return;
		}
		PackStatis.getToService(PackStatis.EventAction.CLICKCHANGE.getValue(),PackStatis.EventCategory.MARKET.getValue(),null,null);
		if (mOneLayout.getVisibility() == View.GONE) {
			mOneLayout.setVisibility(View.VISIBLE);
			mTwoLayout.setVisibility(View.VISIBLE);
			mArrowup.setImageResource(R.mipmap.a_small_developdown);
		} else {
			mOneLayout.setVisibility(View.GONE);
			mTwoLayout.setVisibility(View.VISIBLE);
			mArrowup.setImageResource(R.mipmap.a_small_developup);
		}
		MobclickEventUtlis.MobclickEventByAccountType(mOwnerAct,"CilckChange");
		PackStatis.getToService(PackStatis.EventAction.CLICKCHANGE.getValue(),PackStatis.EventCategory.MARKET.getValue(),null,null);
	}
	/**
	 * 设置切换按钮点击监听事件
	 *
	 * @param mCallback
	 */
	public void setAccountChangeViewClickListener(UserInfoHBar.AccountChangeViewClickListener mCallback) {
		this.mCallback = mCallback;
	}
}
